package com.td.erhome.ui.historycost;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.td.erhome.R;
import com.td.erhome.adapter.CostListAdapter;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.BaseBean;
import com.td.erhome.models.CostListBean;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CostListActivity extends BaseActivity {

    @Bind(R.id.img_back)
    ImageView mImgBack;
    @Bind(R.id.et_search)
    EditText mEtSearch;
    @Bind(R.id.recyclview)
    XRecyclerView mRecyclview;
    private int page = 1;
    private long mId;
    private String mName;
    private List<CostListBean.HouseListBean> mHouse_list = new ArrayList<>();
    private CostListAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cost_list;
    }

    @Override
    protected void initView() {
        mName = getIntent().getStringExtra("name");
        mId = getIntent().getLongExtra("id",0);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclview.setLayoutManager(layoutManager);
        mAdapter = new CostListAdapter(this,mHouse_list);
        mRecyclview.setAdapter(mAdapter);


        //搜索框的监听
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //是否是回车键
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    // 先隐藏键盘
                    ((InputMethodManager) mEtSearch.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    mAdapter.clearData();
                    page =1;
                    getData();
                }
                return false;
            }
        });


        mRecyclview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mAdapter.clearData();
                page =1;
                getData();
            }
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });

        getData();

    }

    private void getData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("page_size", 30);
        params.put("area_id", mId/1000);
        params.put("keyword", mEtSearch.getText().toString());
        Api.getRetrofit().getSecondHouseBuyRecord(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new BaseSubscriber<BaseBean<CostListBean>>(CostListActivity.this){
                   @Override
                   public void onCompleted() {
                       super.onCompleted();
                       mRecyclview.refreshComplete();
                   }
                   @Override
                   public void onError(Throwable e) {
                       super.onError(e);
                       mRecyclview.refreshComplete();
                   }

                   @Override
                   public void onNext(BaseBean<CostListBean> costListBeanBaseBean) {
                       super.onNext(costListBeanBaseBean);
                       mHouse_list = costListBeanBaseBean.getData().house_list;
                       mAdapter.AddAllData(mHouse_list);
                       mRecyclview.refreshComplete();

                   }
               });
    }

    @Override
    protected void releaseResource() {

    }


    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
